package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Term;
import com.example.result.Result;
import com.example.service.TermService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 学期控制层
 * @date 2020-01-01
 */
@RestController
@RequestMapping("/api")
public class TermController {

    @Autowired
    TermService termService;

    //==================================学期管理===============================================

    /**
     * @param json
     * @return 添加成功则返回成功提示信息，反之则提示添加失败信息
     * @describe 添加学期信息
     */
    @RequestMapping(value = "/terms", method = RequestMethod.POST)
    public Result addTerm(@RequestBody String json) {

        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Term term = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Term.class);

        return termService.addTerm(term);
    }

    /**
     * @param json
     * @return 更新成功则返回成功提示信息，反之则提示更新失败信息
     * @describe 更新学期信息
     */
    @RequestMapping(value = "/terms", method = RequestMethod.PUT)
    public Result updateTerm(@RequestBody String json) {

        //将JSON数据转换为Course对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Term term = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Term.class);

        return termService.updateTerm(term);
    }

    /**
     * @param termId
     * @return 删除成功则返回成功提示信息，反之则提示删除失败信息
     * @describe 根据编号删除学期信息
     */
    @RequestMapping(value = "/terms/{termId}", method = RequestMethod.DELETE)
    public Result deleteTermById(@PathVariable("termId") String termId) {

        return termService.deleteTermById(termId);
    }

    /**
     * @return 以JSON格式返回所有学期信息
     * @describe 查询所有学期信息
     */
    @RequestMapping(value = "/terms", method = RequestMethod.GET)
    public Result queryAllTerm() {

        return termService.queryAllTerm();
    }

    /**
     * @param termId
     * @return 以JSON格式返回该学期信息
     * @describe 根据编号查询该学期信息
     */
    @RequestMapping(value = "/terms/{termId}", method = RequestMethod.GET)
    public Result queryTermById(@PathVariable("termId") String termId) {

        return termService.queryTermById(termId);
    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加学期信息
     */
    @RequestMapping(value = "/terms/excels", method = RequestMethod.POST)
    public Result addTermByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Term> list = JSON.parseArray(parseObject.getString("json"), Term.class);

        System.out.println(list);

        return termService.addTermByExcel(list);

    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 学期信息模糊查询
     */
    @RequestMapping(value = "/terms/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return termService.serachByInfo(info);

    }

    /**
     * @param termIds
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学期编号批量删除学期信息
     */
    @RequestMapping(value = "/terms/dels", method = RequestMethod.POST)
    public Result deleteTermByIds(@RequestBody Map<String, String> termIds) {

        return termService.deleteTermByIds(termIds);
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学期名称和ID
     */
    @RequestMapping(value = "/terms/names", method = RequestMethod.GET)
    public Result queryAllTermNameAndId() {

        return termService.queryAllTermNameAndId();

    }

}
